<?php
/**
 * Created by PhpStorm.
 * User: ytl003
 * Date: 2018/7/26
 * Time: 下午11:15
 */

namespace app\api\controller\v1;


use think\Session;

class Cet
{
    /**
     * 通过准考证获取验证码url并把查询时要传回的cookie设置进入seesion
     * @param $ik 考号
     * @return \think\response\Json
     */
    public function getValidateImg($ik){
        if ($ik=='') {
            return json(['code' => -2, 'msg' => '准考证号都不给我，验证码我也不给你']);
        }
        $url = "http://cache.neea.edu.cn/Imgs.do?c=CET&ik=" . $ik . "&t=" . lcg_value();
        $rs = $this->curl_request($url, "", "", "http://cet.neea.edu.cn/cet/", true);
        if (preg_match("/Set-Cookie:(.*?);/", $rs, $cookies) && preg_match('/result.imgs\(\"(.*?)\"/', $rs, $rs1)) {
            $cookie = $cookies[1];
//            echo $rs;
            Session::set("cookie", $cookie);
            $img = $rs1[1];
            return json(['code' => 0, 'msg' => '获取成功', 'data' => $img]);
        }
        return json(['code' => -1, 'msg' => '获取验证码失败']);
    }

    /**
     * 查询四六级成绩
     * @param $ik 考号
     * @param $name 名字
     * @param $verify 验证码
     * @return \think\response\Json
     */
    public function getGrades($ik, $name, $verify){
        if (strlen($ik) != 15) {
            return json(['code' => 0, 'msg' => '准考证号输错了']);
        }
        $year = substr($ik, 6, 3);
        $level = substr($ik, 9, 1);
        $a = $level == 1 ? "CET4_" . $year . "_DANGCI," . $ik . "," . $name : "CET6_" . $year . "_DANGCI," . $ik . "," . $name;
        $post = [
            "data" => $a, "v" => $verify
        ];
        $cookie = Session::get("cookie");
        $rs = $this->curl_request('http://cache.neea.edu.cn/cet/query', $cookie, $post, "http://cet.neea.edu.cn/cet/");
        preg_match('/{(.*?)}/', $rs, $rs1);
//        return $rs;
        return json(['code' => 0, 'msg' => '查询成功', 'data' => $rs1[0]]);
    }

    public function test() {
//        $str = "{z:'520100172104611',n:'郑洪鑫',x:'贵阳学院',s:293.00,t:0,l:87,r:121,w:85,kyz:'--',kys:'--'}";
//        preg_match_all("/'(.*?)'/",$str,$re);
//        preg_match_all("/:(.*?),/",$str,$re1);
//
//        if ($re[1][3] == '--'){
//            $data = [
//                'z' => $re[1][0],
//                'n' => $re[1][1],
//                'x' => $re[1][2],
//                's' => $re1[1][3],
//                't' => $re1[1][4],
//                'l' => $re1[1][5],
//                'r' => $re1[1][6],
//                'w' => $re1[1][7],
//                'kyz' => $re[1][3],
//                'kys' => $re[1][4],
//            ];
//        }else{
//            $data = [
//                'z' => $re[1][0],
//                'n' => $re[1][1],
//                'x' => $re[1][2],
//                's' => $re1[1][3],
//                't' => $re1[1][4],
//                'l' => $re1[1][5],
//                'r' => $re1[1][6],
//                'w' => $re1[1][7],
//                'kyz' => $re1[1][8],
//                'kys' => $re1[1][9],
//            ];
//        }
//
//        var_dump($re);
//        var_dump($re1);
//        var_dump($data);
//        return json($re1);
        return view();
    }

    private function curl_request($url, $cookie = '', $post = '', $referer = "", $header = false)
    {
        $ip = virtualIp();
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36');
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'CLIENT-IP:' . $ip,
            'X-FORWARDED-FOR:' . $ip,
            'Host:cet.neea.edu.cn'
        ));
        if ($header) {
            curl_setopt($curl, CURLOPT_HEADER, $header);
        }
        if ($referer) {
            curl_setopt($curl, CURLOPT_REFERER, $referer);
        }
        if ($cookie) {
            curl_setopt($curl, CURLOPT_COOKIE, $cookie);
        }
        if ($post) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
        }
        curl_setopt($curl, CURLOPT_TIMEOUT, 14);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        if (curl_errno($curl)) {
            return curl_error($curl);
        }
        curl_close($curl);
        return $data;
    }
}